# AlbumScreen - This screen allows user to browse and play tracks of the album
# Copyright (C) 2007 Lauri Taimila
# 
# Entertainer is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Entertainer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import clutter

from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

# Widgets
from frontend.gui.widgets.text_menu import TextMenu
from frontend.gui.widgets.text_menu_item import TextMenuItem

from frontend.gui.widgets.list_indicator import ListIndicator

# Music library
from frontend.medialibrary.music import MusicLibrary
from frontend.medialibrary.music import Track
from frontend.medialibrary.music import Album

class AlbumScreen(Screen):
    """
    AlbumScreen
    
    Screen that allows user to browse and play tracks of the music album.
    """
    
    def __init__(self, ui, frontend, album):
        """
        Initialize screen
        
        @param ui: UserInterface object
        @param frontend: FrontendClient object
        @param album: Album object (We display information of this album)
        """
        self.ui = ui
        self.frontend = frontend
        self.theme = frontend.getTheme()
        self.library = self.frontend.get_music_library()
        self.group = clutter.Group()
        self.album = album
        
        # Screen Title (Displayed at the bottom left corner)
        screen_title = clutter.Label()
        screen_title.set_font_name(self.theme.getFont() + ' 100px')
        screen_title.set_color(self.theme.getColor("screentitle"))
        screen_title.set_text(self.album.get_title())
        screen_title.set_name("screen_title")
        (screen_title_width, screen_title_height) = screen_title.get_size()
        screen_title_x = 0
        screen_title_y = ui.STAGE_HEIGHT - screen_title_height + 20
        screen_title.set_position(screen_title_x, screen_title_y)
        screen_title.show()
        self.group.add(screen_title)
        
        # Create and initialize screen items
        self._create_track_menu()
        self._create_album_info()
        self._create_album_cover_texture()
        
        #List indicator
        self.li = ListIndicator(120,34, self.theme, ListIndicator.VERTICAL)
        li_y = self.frontend.getUserInterface().STAGE_HEIGHT - 150
        li_x = self.frontend.getUserInterface().STAGE_WIDTH - self.li.get_width() -90
        self.li.set_position(li_x,li_y)
        self.li.set_delimiter(" | ")
        self.li.show()
        self.li.set_maximum(self.track_menu.get_number_of_items())
        self.li.set_current(1)
        self.group.add(self.li)
        
        self.group.show()
        ui.getStage().add(self.group)

    def _create_album_cover_texture(self):
        """
        Create a texture that is displayed next to track list. This texture
        displays album cover art.
        """
        if(self.album.has_album_art()):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.album.get_album_art_url())
        else:
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("default_album_art"))
        self.art = clutter.Texture()
        self.art.set_pixbuf(pixbuf)
        self.art.set_position(self.ui.get_abs_x_pos(0.1),self.ui.get_abs_y_pos(0.165))
        self.art.set_size(430,430)
        self.art.show()
        self.group.add(self.art)

    def _create_album_info(self):
        """
        Create album info labels.
        """
        album = clutter.Label()
        album.set_font_name(self.theme.getFont() + ' 32px')
        album.set_color(self.theme.getColor("text"))
        album.set_text(self.album.get_title() + ", " + str(self.album.get_year()))
        album.set_size(500, 50)
        album.set_position(self.ui.get_abs_x_pos(0.1), self.ui.get_abs_y_pos(0.73))
        album.show()
        self.group.add(album)
        
        artist = clutter.Label()
        artist.set_font_name(self.theme.getFont() + ' 28px')
        artist.set_color(self.theme.getColor("subtitle"))
        artist.set_text(self.album.get_artist())
        artist.set_size(500, 50)
        artist.set_position(self.ui.get_abs_x_pos(0.1), self.ui.get_abs_y_pos(0.78))
        artist.show()
        self.group.add(artist)
        
        num_of_tracks = clutter.Label()
        num_of_tracks.set_font_name(self.theme.getFont() + ' 28px')
        num_of_tracks.set_color(self.theme.getColor("subtitle"))
        length = str(self.album.get_total_length() / 60)
        num_of_tracks.set_text(str(self.album.get_number_of_tracks()) + " tracks, " + length + " minutes")
        num_of_tracks.set_size(500, 50)
        num_of_tracks.set_position(self.ui.get_abs_x_pos(0.1), self.ui.get_abs_y_pos(0.83))
        num_of_tracks.show()
        self.group.add(num_of_tracks)
        
        #genre = clutter.Label()
        #genre.set_font_name(self.theme.getFont() + ' 28px')
        #genre.set_color(self.theme.getColor("text"))
        #genre.set_text(self.album.get_genre())
        #genre.set_size(500, 50)
        #genre.set_position(130, self.ui.get_abs_y_pos(0.83))
        #genre.show()
        #self.group.add(genre)

    def _create_track_menu(self):
        """
        Create track menu. This menu contains list of all tracks on album.
        """
        self.track_menu = TextMenu(self.theme, 
                               self.frontend.get_configuration().show_effects())
        self.track_menu.set_row_count(1)
        #self.track_menu.set_visible_column_count(10)
        self.track_menu.set_item_size(600, 60)
        self.track_menu.set_position(680, 120)
        
        # Create menu items based on MusicLibrary
        tracks = self.album.get_tracks()
        for track in tracks:
            track_length = str(track.get_length() / 60) + ":" + str(track.get_length() % 60).zfill(2)
            item = TextMenuItem(600, 60, self.theme, track.get_title(), track_length)
            item.set_userdata(track)
            self.track_menu.add_actor(item)
                
        self.track_menu.show()
        self.track_menu.set_active(True)
        self.group.add(self.track_menu)
        
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.NORMAL
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "AlbumScreen"
    
    def handle_key_press_event(self, event):   
        """ Handle key pressed event."""
        #UP KEY
        if event.keyval == clutter.keysyms.Up:
            self.track_menu.move(TextMenu.UP)
            self.li.set_current(self.track_menu.get_current_postition() + 1)
        
        #DOWN KEY  
        elif event.keyval == clutter.keysyms.Down:
            self.track_menu.move(TextMenu.DOWN)
            self.li.set_current(self.track_menu.get_current_postition() + 1)
        
        #LEFT KEY
        elif event.keyval == clutter.keysyms.Left:
            pass
        
        # RIGHT KEY
        elif event.keyval == clutter.keysyms.Right:
            pass
        
        # RETURN KEY
        elif event.keyval == clutter.keysyms.Return:
            track = self.track_menu.get_current_menuitem().get_userdata()
            self.ui.changeScreen(self.ui.createScreen("audio_play", track), Transition.FORWARD)

